﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AnesSystem.ViewModels;
using AnesSystem.Models;
using System.Windows.Controls.Primitives;

namespace AnesSystem.Controls
{
    /// <summary>
    /// ChuFangTableCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class ChuFangTableCtrl : UserControl
    {

        #region Variables
        #endregion

        #region Constructor
        public ChuFangTableCtrl()
        {

            InitializeComponent();
            this.DataContext = ChuFangTableCtrlViewModel.Instance;
            this.Loaded += ChuFangTableCtrl_Loaded;
        }

        private void ChuFangTableCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            string age = DataManager.Instance.CurrentPatientLink.Patient.Nl;
            try
            {
                age = age.Replace("岁", "");
                if (age != null && int.Parse(age) >= 14)
                {
                    ekHeard.Height = jeHeard.Height;
                    ekDateGrid.Height = jeDateGrid.Height;
                    ekHeard.Height = new GridLength(0);
                    ekDateGrid.Height = new GridLength(0);
                }
                else
                {
                    ekHeard.Height = jeHeard.Height;
                    ekDateGrid.Height = jeDateGrid.Height;
                    ptHeard.Height = new GridLength(0);
                    ptDateGrid.Height = new GridLength(0);
                }
            }
            catch (Exception ea)
            {

            }
            DataManager.Instance.IsNotNeedSaveData = false;
            if (OperationDataPool.ChangePatient == false)
            {
                ChuFangTableCtrlViewModel.Instance.RefResh();
            }
        }


        #endregion

        #region Properties

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods

        #endregion

        #region EventHandlers

        private void addEvent(DataGrid myDataGrid)
        {
            myDataGrid.Focus();
            myDataGrid.CurrentColumn = myDataGrid.Columns[0];
            myDataGrid.ScrollIntoView(myDataGrid.Items[myDataGrid.Items.Count - 1], myDataGrid.CurrentColumn);
            DataGridTemplateColumn tempColumn = myDataGrid.Columns[0] as DataGridTemplateColumn;
            //然后获取DataGridTemplateColumn单元格元素
            FrameworkElement element = myDataGrid.Columns[0].GetCellContent(myDataGrid.Items[myDataGrid.Items.Count - 1]);
            if (element != null)
            {
                //把单元格元素转换为相应的控件，再从该控件中取值
                CustomDrugsCmbCtrl ck = tempColumn.CellTemplate.FindName("waiceng", element) as CustomDrugsCmbCtrl;
                if (ck != null)
                    ck._cmb._txtName._txt.Focus();
                //do something here
            }
            //DataGridCell cell = GetDataGridCell(myDataGrid.Items.Count - 1, 0);
            //CustomDrugsCmbCtrl cmb = GetVisualChild<CustomDrugsCmbCtrl>(myDataGrid, v => v.Name == "drugsCmb");
            //if (null != cmb)
            //{
            //    cmb.Focus();
            //}
            //CheckBox chb = GetVisualChild<CheckBox>(myDataGrid, v => v.Name == "cbbSelALL");
            //if (null != chb)
            //{
            //    chb.IsChecked = false;
            //}
        }

        //普通增删
        private void btnAddPt_Click(object sender, RoutedEventArgs e)
        {
            ChuFangTableCtrlViewModel.Instance.AddChuFang("普通");
            addEvent(_puTongDataGrid);
        }
        private void btnDeletePt_Click(object sender, RoutedEventArgs e)
        {
            ChuFangItemModel model = (sender as Button).DataContext as ChuFangItemModel;
            ChuFangTableCtrlViewModel.Instance.RemoveChuFang(model);
        }

        ///// <summary>
        ///// 得到DataGrid的一个单元格
        ///// </summary>
        ///// <param name="rowIndex">行索引</param>
        ///// <param name="cellIndex">列索引</param>
        ///// <returns></returns>
        //private DataGridCell GetDataGridCell(DataGrid myDataGrid, int rowIndex, int cellIndex)
        //{
        //    DataGridRow row = (DataGridRow)myDataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
        //    myDataGrid.UpdateLayout();
        //    row = (DataGridRow)myDataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
        //    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
        //    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(cellIndex);
        //    myDataGrid.ScrollIntoView(row, myDataGrid.Columns[cellIndex]);
        //    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(cellIndex);

        //    return cell;
        //}

        public T GetVisualChild<T>(DependencyObject parent, Func<T, bool> predicate) where T : Visual
        {
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                DependencyObject v = (DependencyObject)VisualTreeHelper.GetChild(parent, i);
                T child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v, predicate);
                    if (child != null)
                    {
                        return child;
                    }
                }
                else
                {
                    if (predicate(child))
                    {
                        return child;
                    }
                }
            }
            return null;
        }
        
        /// <summary>   
        /// 获取父可视对象中第一个指定类型的子可视对象   
        /// </summary>   
        /// <typeparam name="T">可视对象类型</typeparam>   
        /// <param name="parent">父可视对象</param>   
        /// <returns>第一个指定类型的子可视对象</returns>   
        public static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }



        //精二增删
        private void btnAddJe_Click(object sender, RoutedEventArgs e)
        {
            ChuFangTableCtrlViewModel.Instance.AddChuFang("精2");
            addEvent(_jingErDataGrid);
        }
        private void btnDeleteJe_Click(object sender, RoutedEventArgs e)
        {
            ChuFangItemModel model = (sender as Button).DataContext as ChuFangItemModel;
            ChuFangTableCtrlViewModel.Instance.RemoveChuFang(model);
        }


        //材料增删
        private void btnAddCl_Click(object sender, RoutedEventArgs e)
        {
            ChuFangTableCtrlViewModel.Instance.AddChuFang("材料");
            addEvent(_caiLiaoDataGrid);
        }
        private void btnDeleteCl_Click(object sender, RoutedEventArgs e)
        {
            ChuFangItemModel model = (sender as Button).DataContext as ChuFangItemModel;
            ChuFangTableCtrlViewModel.Instance.RemoveChuFang(model);
        }


        //儿科增删
        private void btnAddEk_Click(object sender, RoutedEventArgs e)
        {
            ChuFangTableCtrlViewModel.Instance.AddChuFang("儿科");
            addEvent(_erKeDataGrid);
        }
        private void btnDeleteEk_Click(object sender, RoutedEventArgs e)
        {
            ChuFangItemModel model = (sender as Button).DataContext as ChuFangItemModel;
            ChuFangTableCtrlViewModel.Instance.RemoveChuFang(model);
        }

        #endregion

        private void readAnesDrugs(object sender, RoutedEventArgs e)
        {
            ChuFangTableCtrlViewModel.Instance.readAnesDrugs();
        }

        private void PrintingPreviewPreview(object sender, RoutedEventArgs e)
        {
            ChuFangPrintPreviewCtrl _printingPreview = new ChuFangPrintPreviewCtrl();
            _printingPreview._chuFangPageList.ItemsSource = ChuFangTableCtrlViewModel.Instance.CreateChuFangPrintData();

            PreviewPrintWindow.Instance.PrintContent = _printingPreview;
            PreviewPrintWindow.Instance.ShowDialog();
        }
    }
}
